﻿using System;
using System.Reflection;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using iMaker.Library;
using System.IO;
using System.Collections;
using System.Web.SessionState;
using HKEA.CMS.Core.DALFactory;
using HKEA.CMS.Core.Models;
using System.Text.RegularExpressions;
namespace HKEA.CMS.Core.Utility
{
    public class Common
    {
        public static T GetInstance<T>(string ClassName)
        {
            object ect = System.Reflection.Assembly.Load("HKEA.CMS.Core").CreateInstance("HKEA.CMS.Core.Models." + ClassName);
            return (T)ect;
        }
        public static string GetManageName()
        {
            string ret = string.Empty;
            if (System.Web.HttpContext.Current.Session[Constant.SessionName.Manage] != null)
            {
                ret = (System.Web.HttpContext.Current.Session[Constant.SessionName.Manage] as tblUser).User_Name;
            }
            else
            { ret = "system"; }
            return ret;
        }
        public class LanguageInfo
        {
            public const string CNLanguage = "zh-cn";
            public const string ENUSLanguage = "en-us";
            public const string ZHHKLanguage = "zh-hk";

            public const string DefaultLanguage = CNLanguage;
            public const string CookieNameOfLanguage = "Language";            

            public static string GetLanguage()
            {
                string strLanguage = DefaultLanguage;
                if (System.Web.HttpContext.Current.Request.Cookies[CookieNameOfLanguage] != null)
                {
                    strLanguage = System.Web.HttpContext.Current.Request.Cookies[CookieNameOfLanguage].Value;
                }
                else
                {
                    SetLanguageCookie(CNLanguage);
                }
                return strLanguage;
            }
            public static void SetLanguageCookie(string Language)
            {
                string strLnaguageValue = string.Empty;
                HttpCookie cookie = new HttpCookie(CookieNameOfLanguage);
                if (Language.ToLower() == ENUSLanguage)
                    cookie.Value = Language.ToLower();
                cookie.Value = CNLanguage;
            }
        }

        /// <summary>
        /// 通用图片上传  一张图片多种尺寸(37*37 50*50 100*100 150*150 200*200) 而且是正方形的
        /// </summary>
        /// <param name="postedFile"></param>
        /// <param name="filePath">上传文件的绝对路径 如：E:\image\logo</param>
        /// <param name="param">可选参数 自定义的前缀</param>
        /// <returns>返回图片的文件名前缀（不包括后缀）比如：abc，要获取真实的名字 自己组合 如:abc_37.jpg abc_50.jpg...；返回是空字符说明上传失败</returns>
        public static string CommonUploadImagesSquare(System.Web.HttpPostedFile postedFile, string filePath,params string[] param)
        {
            return CommonUploadImagesSquareWithName(postedFile, filePath, "", param);
        }

        /// <summary>
        /// 通用图片上传  一张图片多种尺寸(37*37 50*50 100*100 150*150 200*200) 而且是正方形的
        /// </summary>
        /// <param name="postedFile">文件</param>
        /// <param name="filePath">上传文件的绝对路径 如：E:\image\logo</param>
        /// <param name="fileName">指定名字 上传图片名字 按照指定名字生成 这时param参数无效！适合修改图片</param>
        /// <param name="param">可选参数 自定义的前缀</param>
        /// <returns></returns>
        public static string CommonUploadImagesSquareWithName(System.Web.HttpPostedFile postedFile, string filePath, string fileName,params string[] param)
        {
            string ret = string.Empty;
            if (postedFile != null && !string.IsNullOrEmpty(filePath))
            {
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                string file_prefix = string.Empty;

                if (!string.IsNullOrEmpty(fileName))
                    file_prefix = fileName;
                else
                {
                    file_prefix = DateTime.Now.ToString("yyyyMMddhhmmss");

                    if (param.Count() > 0)
                    {
                        string tmp = string.Empty;
                        foreach (string str in param) {
                            tmp += str;
                        }
                        file_prefix = tmp + "_" + file_prefix;
                    }
                }
                FileOperate.ResizeImage(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "37_37", "jpg"), 37, 37);
                FileOperate.ResizeImage(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "50_50", "jpg"), 50, 50);
                FileOperate.ResizeImage(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "100_100", "jpg"), 100, 100);
                FileOperate.ResizeImage(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "150_150", "jpg"), 150, 150);
                FileOperate.ResizeImage(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "200_200", "jpg"), 200, 200);

                //PTImage.CutForSquare(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "37_37", "jpg"), 37, 100);
                //PTImage.CutForSquare(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "50_50", "jpg"), 50, 100);
                //PTImage.CutForSquare(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "100_100", "jpg"), 100, 100);
                //PTImage.CutForSquare(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "150_150", "jpg"), 150, 100);
                //PTImage.CutForSquare(postedFile, string.Format("{0}\\{1}_{2}.{3}", filePath, file_prefix, "200_200", "jpg"), 200, 100);
                ret = file_prefix;
            }
            return ret;
        }

        public static string ReturnProductCard(string str) {
            switch (str)
            {
                case "A":
                    str = "Textile & Garment";
                    break;
                case "B":
                    str = "Electronics, AV & IT";
                    break;
                case "C":
                    str = "Houseware";
                    break;
                case "D":
                    str = "Gifts & Premium";
                    break;
                case "E":
                    str = "Others";
                    break;
            }
            return str;
        }

        public static string ReturnBoolValue(string val)
        {
            string str = string.Empty;
            switch (val)
            {
                case "1":
                    str = "YES";
                    break;
                case "0":
                    str = "NO";
                    return str;
            }
            return str;
        }

        #region 检查字符串,防止Sql注入
        /// <summary>
        /// 检查输入的特殊字符串
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string CheckParams_chang(string args)
        {
            string[] Lawlesses = { "*","exec","and","20%and20%","master","20%master20%", "=", "net user", "xp_cmdshell", "/add", "exec master.dbo.xp_cmdshell", "net localgroup administrators", "select", "count", "Asc", "char", "mid", "'", ":", "\"", "insert", "delete from","declare","function", "drop", "drop table", "update", "truncate", "from", "%", "UNION" };
            string str_Regex = ".*[";
            for (int i = 0; i < Lawlesses.Length - 1; i++)
                str_Regex += Lawlesses[i] + "|";
            str_Regex += Lawlesses[Lawlesses.Length - 1] + "].*";
            if (Regex.Matches(args, str_Regex).Count > 0)
            {
                return '0'.ToString();
            }
            else
            {
                return args;
            }
        }

        public static string CheckParams(string str)
        {
            string result = System.Text.RegularExpressions.Regex.Replace(str.Trim(), @" +", " ").ToLower();
            string pattern = @"select|insert|master|delete|from|count\(|drop|drop table|table|update|truncate|asc\(|mid\(|char\(|xp_cmdshell|exec|exec master.dbo.xp_cmdshell|net localgroup administrators|:|net user|""|union|or|and|delete from|declare|function|where";
            if (Regex.IsMatch(str, pattern, RegexOptions.IgnoreCase) || Regex.IsMatch(str, @"[-|;|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\'|=]"))
            {
                return "0";
            }
            else
            {
                return result;
            }
        }
        
        #endregion
    }
}
